﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SHomeWorkshop.LunarMind.Dialogs
{
    /// <summary>
    /// LunarMsgBox.xaml 的交互逻辑
    /// </summary>
    public partial class LunarMsgBox : Window
    {
        /// <summary>
        /// 显示一个自定义的信息框。
        /// </summary>
        /// <param name="headerText"></param>
        /// <param name="contentText"></param>
        /// <param name="detailsText"></param>
        /// <param name="buttons"></param>
        /// <param name="image"></param>
        /// <param name="footText"></param>
        /// <param name="defResult">如果和buttons不匹配，则会自动提供匹配的值。</param>
        /// <returns></returns>
        public static MessageBoxResult MsgBoxShow(
            string title,
            string headerText,
            string contentText,
            string detailsText = null,
            MessageBoxButton buttons = MessageBoxButton.OK,
            MessageBoxImage image = MessageBoxImage.Information,
            string footText = "", MessageBoxResult defResult = MessageBoxResult.Cancel,
            Window ownerWindow = null)
        {
            LunarMsgBox lmb = new LunarMsgBox(title, headerText, contentText, detailsText,
                buttons, image, footText, defResult, ownerWindow);
            lmb.ShowDialog();
            return lmb.Result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="headerText"></param>
        /// <param name="contentText"></param>
        /// <param name="detailsText"></param>
        /// <param name="buttons"></param>
        /// <param name="image"></param>
        /// <param name="footText"></param>
        /// <param name="defResult"></param>
        /// <param name="ownerWindow">如果不是由Lunar Presentation调用，必须传入此参数。</param>
        public LunarMsgBox(
            string title,
            string headerText,
            string contentText,
            string detailsText,
            MessageBoxButton buttons,
            MessageBoxImage image,
            string footText,
            MessageBoxResult defResult, Window ownerWindow = null)
        {
            InitializeComponent();

            if (ownerWindow != null)
            {
                Owner = ownerWindow;
            }
            else
            {
                Owner = Application.Current.MainWindow;
            }

            this.Title = title;

            #region 注意：这里需要用私有的属性而不能是字段。
            HeaderText = headerText;
            ContentText = contentText;
            DetailsText = detailsText;
            FooterText = footText;

            this.Buttons = buttons;
            this.ImageType = image;

            #endregion

            InitializeResult(defResult);

            tbDetails.Visibility = Visibility.Collapsed;

            switch (image)
            {
                case MessageBoxImage.Error:
                    {
                        System.Media.SystemSounds.Hand.Play(); break;
                    }
                case MessageBoxImage.Information:
                    {
                        System.Media.SystemSounds.Asterisk.Play(); break;
                    }
                case MessageBoxImage.Question:
                    {
                        System.Media.SystemSounds.Question.Play(); break;
                    }
                case MessageBoxImage.Warning:
                    {
                        System.Media.SystemSounds.Exclamation.Play(); break;
                    }
                default:
                    {
                        System.Media.SystemSounds.Beep.Play(); break;
                    }
            }
        }

        private string HeaderText
        {
            set
            {
                if (value != null && value.Length > 0)
                {
                    tbHeader.Text = value;
                }
                else
                {
                    tbHeader.Text = "…<无内容>…";
                }
            }
        }

        private string ContentText
        {
            set
            {
                if (value != null && value.Length > 0)
                {
                    tbContent.Text = value;
                    tbContent.Visibility = Visibility.Visible;
                }
                else
                {
                    tbContent.Text = "";
                    tbContent.Visibility = Visibility.Collapsed;
                }
            }
        }

        private string DetailsText
        {
            set
            {
                if (value != null && value.Length > 0)
                {
                    ckxShowDetails.Visibility = Visibility.Visible;
                    tbDetails.Visibility = Visibility.Collapsed;

                    tbDetails.Text = value;
                }
                else
                {
                    ckxShowDetails.Visibility = Visibility.Hidden;
                    tbDetails.Visibility = Visibility.Collapsed;

                    tbDetails.Text = "";
                }
            }
        }

        private string FooterText
        {
            set
            {
                if (value != null && value.Length > 0)
                {
                    tbFooter.Visibility = Visibility.Visible;
                    tbFooter.Text = value;
                }
                else
                {
                    tbFooter.Visibility = Visibility.Collapsed;
                    tbFooter.Text = "";
                }
            }
        }

        private MessageBoxButton buttons = MessageBoxButton.OK;

        private MessageBoxButton Buttons
        {
            set
            {
                buttons = value;
                RefreshButtonsStatus(value);
            }
        }

        private void RefreshButtonsStatus(MessageBoxButton buttons)
        {
            switch (buttons)
            {
                case MessageBoxButton.OK:
                    {
                        btn1.Visibility = Visibility.Visible;
                        btn2.Visibility = Visibility.Collapsed;
                        btn3.Visibility = Visibility.Collapsed;

                        btn1.Tag = "OK"; btn1.Content = "确定(_O)";

                        this.result = MessageBoxResult.OK;//默认值；

                        btn1.IsDefault = true;
                        btn2.IsDefault = false;
                        btn3.IsDefault = false;

                        btn1.IsCancel = false;
                        btn2.IsCancel = false;
                        btn3.IsCancel = false;

                        break;
                    }
                case MessageBoxButton.OKCancel:
                    {
                        btn1.Visibility = Visibility.Visible;
                        btn2.Visibility = Visibility.Visible;
                        btn3.Visibility = Visibility.Collapsed;

                        btn1.Tag = "OK"; btn1.Content = "确定(_O)";
                        btn2.Tag = "Cancel"; btn2.Content = "取消(_C)";

                        this.result = MessageBoxResult.OK;//默认值；


                        btn1.IsDefault = true;
                        btn2.IsDefault = false;
                        btn3.IsDefault = false;

                        btn1.IsCancel = false;
                        btn2.IsCancel = true;
                        btn3.IsCancel = false;

                        break;
                    }
                case MessageBoxButton.YesNo:
                    {
                        btn1.Visibility = Visibility.Visible;
                        btn2.Visibility = Visibility.Visible;
                        btn3.Visibility = Visibility.Collapsed;

                        btn1.Tag = "Yes"; btn1.Content = "是(_Y)";
                        btn2.Tag = "No"; btn2.Content = "否(_N)";


                        btn1.IsDefault = false;
                        btn2.IsDefault = false;
                        btn3.IsDefault = false;

                        btn1.IsCancel = false;
                        btn2.IsCancel = true;
                        btn3.IsCancel = false;
                        break;
                    }
                case MessageBoxButton.YesNoCancel:
                    {
                        btn1.Visibility = Visibility.Visible;
                        btn2.Visibility = Visibility.Visible;
                        btn3.Visibility = Visibility.Visible;

                        btn1.Tag = "Yes"; btn1.Content = "是(_Y)";
                        btn2.Tag = "No"; btn1.Content = "否(_N)";
                        btn3.Tag = "Cancel"; btn1.Content = "取消(_C)";

                        btn1.IsDefault = false;
                        btn2.IsDefault = false;
                        btn3.IsDefault = false;

                        btn1.IsCancel = false;
                        btn2.IsCancel = false;
                        btn3.IsCancel = true;

                        break;
                    }
            }
        }

        private MessageBoxImage ImageType
        {
            set
            {
                if (value == MessageBoxImage.None)
                {
                    iconImage.Visibility = Visibility.Collapsed;
                }
                else
                {
                    iconImage.Visibility = Visibility.Visible;
                }

                BuildImageSource(value);
            }
        }

        private MessageBoxResult result = MessageBoxResult.OK;

        public MessageBoxResult Result
        {
            get { return result; }
            set { result = value; }
        }

        /// <summary>
        /// 为右上角关闭按钮才提供这个属性。
        /// </summary>
        private void InitializeResult(MessageBoxResult defResult)
        {
            switch (buttons)
            {
                case MessageBoxButton.OK:
                    {
                        result = MessageBoxResult.OK;//仅此一个选项
                        btn1.Focus();
                        break;
                    }
                case MessageBoxButton.OKCancel:
                    {
                        if (defResult == MessageBoxResult.OK)
                        {
                            result = defResult;
                            btn1.Focus();
                        }
                        if (defResult == MessageBoxResult.Cancel)
                        {
                            result = defResult;
                            btn2.Focus();
                        }
                        else
                        {
                            result = MessageBoxResult.Cancel;
                            btn2.Focus();
                            //对于OkCancel/YesNo型的消息框，点击右上角关闭按钮时，返回的应是默认值。
                        }

                        break;
                    }
                case MessageBoxButton.YesNo:
                    {
                        if (defResult == MessageBoxResult.Yes)
                        {
                            result = defResult;
                            btn1.Focus();
                        }
                        if (defResult == MessageBoxResult.No)
                        {
                            result = defResult;
                            btn2.Focus();
                        }
                        else
                        {
                            result = MessageBoxResult.No;
                            btn2.Focus();
                            //对于OkCancel/YesNo型的消息框，点击右上角关闭按钮时，返回的应是默认值。
                        }
                        break;
                    }
                case MessageBoxButton.YesNoCancel:
                    {
                        if (defResult == MessageBoxResult.Yes)
                        {
                            result = defResult;
                            btn1.Focus();
                        }
                        if (defResult == MessageBoxResult.No)
                        {
                            result = defResult;
                            btn2.Focus();
                        }
                        if (defResult == MessageBoxResult.Cancel)
                        {
                            result = defResult;
                            btn3.Focus();
                        }
                        else
                        {
                            result = MessageBoxResult.Cancel;
                            btn3.Focus();
                            //对于OkCancel/YesNo型的消息框，点击右上角关闭按钮时，返回的应是默认值。
                        }
                        break;
                    }
            }
        }

        private void BuildImageSource(MessageBoxImage imgType)
        {
            switch (imgType)
            {
                case MessageBoxImage.Error:
                    {
                        iconImage.Source = new BitmapImage(new Uri(
                            "pack://application:,,,/LunarMind;component/Resources/LunarMsgBoxIcons/error.png"));

                        break;
                    }
                case MessageBoxImage.Warning:
                    {
                        iconImage.Source = new BitmapImage(new Uri(
                            "pack://application:,,,/LunarMind;component/Resources/LunarMsgBoxIcons/Warning.png"));
                        break;
                    }
                case MessageBoxImage.Question:
                    {
                        iconImage.Source = new BitmapImage(new Uri(
                            "pack://application:,,,/LunarMind;component/Resources/LunarMsgBoxIcons/question.png"));
                        break;
                    }
                case MessageBoxImage.Information:
                    {
                        iconImage.Source = new BitmapImage(new Uri(
                            "pack://application:,,,/LunarMind;component/Resources/LunarMsgBoxIcons/infomation.png"));
                        break;
                    }
                default:
                    {
                        iconImage.Source = null;
                        break;
                    }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn == null) return;

            string tag = btn.Tag as string;
            if (tag == null)
            {
                this.result = MessageBoxResult.OK;
                this.DialogResult = false;
                this.Close();
            }

            try
            {
                this.result = (MessageBoxResult)Enum.Parse(typeof(MessageBoxResult), tag);
                this.DialogResult = true;
                this.Close();
            }
            catch
            {
                this.Close();
            }
        }

        private void ckxShowDetails_Checked(object sender, RoutedEventArgs e)
        {
            tbDetails.Visibility = Visibility.Visible;
        }

        private void ckxShowDetails_Unchecked(object sender, RoutedEventArgs e)
        {
            tbDetails.Visibility = Visibility.Collapsed;
        }

        private void cmiCopyText1_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetData(DataFormats.UnicodeText, tbContent.SelectedText);
        }

        private void cmiCopyText2_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetData(DataFormats.UnicodeText, tbDetails.SelectedText);
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("\r\n====Header===========================\r\n");
            sb.Append(tbHeader.Text);
            sb.Append("\r\n====Content==========================\r\n");
            sb.Append(tbContent.Text);
            sb.Append("\r\n====Details==========================\r\n");
            sb.Append(tbDetails.Text);
            sb.Append("\r\n====End==============================\r\n");

            Clipboard.SetData(DataFormats.UnicodeText, sb.ToString());
            MessageBox.Show("已将文本复制到 Windows 系统剪贴板中！",
                Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void Border_MouseEnter(object sender, MouseEventArgs e)
        {
            bdCopy.BorderBrush = Brushes.Red;
        }

        private void Border_MouseLeave(object sender, MouseEventArgs e)
        {
            bdCopy.BorderBrush = Brushes.Black;
        }

    }
}
